﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace SharpMap.Wpf.Converters
{
    public class StringLayerSourceConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string path = value as string;
            if (targetType.IsAssignableFrom(typeof(SharpMap.Layers.GeometryLayer)))
            {
                if (path != null)
                {
                    string fullPath = path;
                    SharpMap.Data.Providers.ShapeFile.ShapeFileProvider shapFile =
                    new SharpMap.Data.Providers.ShapeFile.ShapeFileProvider(fullPath);
                    
                    SharpMap.Layers.GeometryLayer layer = new SharpMap.Layers.GeometryLayer(path, shapFile);
                    shapFile.Open();
                    return layer;

                }
            }
            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion


        #region private 
        string GetAbsolutePath(string path)
        {
            String exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

            exePath = exePath.Remove(exePath.LastIndexOf('\\'));
            String fileFullPath = exePath + "\\" + path;

            return fileFullPath;
            
        }
        #endregion
    }
}
